package com.pbc.gmp.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.pbc.gmp.commons.utils.permissionBitUtil;
import com.pbc.gmp.dao.MenuResMapper;
import com.pbc.gmp.dao.RoleMapper;
import com.pbc.gmp.model.basemodel.Acl;
import com.pbc.gmp.dao.AclMapper;
import com.pbc.gmp.model.basemodel.MenuRes;
import com.pbc.gmp.model.mybatisDto.AclTotalDto;
import com.pbc.gmp.service.IAclService;

import com.xiaoleilu.hutool.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * é‰å†®æªºéŽºÑƒåŸ—é¨å‹«å§é–¿î†¾è¢«é”›å²ƒç¹–æ¶“î†è¢«é¢ã„¦æ½µç€›æ¨ºåæ¶“è®³ç¶‹éœå²ƒç¥«å©§æ„ªç®£é—‚å¯¸æ®‘éå´‡éƒ´é”›å²€æ•¤é‰ãƒ§â€˜ç€¹æ°«å¯Œæµ£æ’¹å…˜æ¾¶ç†·î‡®éîƒç°ºç’§å‹¬ç°®æ©æ¶œî”‘éîƒç°ºéŽ¿å¶„ç¶”  服务实现类
 * </p>
 *
 * @author kjk
 * @since 2017-04-17
 */
@Service
public class AclServiceImpl extends ServiceImpl<AclMapper, Acl> implements IAclService {
    @Autowired
    private IAclService aclService;
    @Resource(name="roleDao")
    private RoleMapper roleMapper;
    @Resource(name = "permissionDao")
    private AclMapper aclMapper;
    @Resource(name="MenuResDao")
    private MenuResMapper menuResMapper;

    @Override
    public Map<String, Set<String>> selectResourceMapByUserId(Long id) {
        Map<String, Set<String>> resourceMap = new HashMap<String, Set<String>>();
        List<Long> roleIdList = roleMapper.selectRoleIdListByUserId(id);
        Set<String> permissions= new HashSet<String>();
        Set<String> roles = new HashSet<String>();
        for (Long roleId : roleIdList) {
          Set<String> permissionList = aclMapper.selectResourceListByRoleId(roleId);

            if (permissionList != null) {
                for (String permission : permissionList) {
                    if (permission==null||permission.equals("")) {

                    }else {
                        permissions.add(permission);
                    }
                }
            }

        }
        resourceMap.put("permissions", permissions);
        resourceMap.put("roles", roleMapper.selectRolesListByUserId(id));
        return  resourceMap;
    }

    @Override
    public List<AclTotalDto> selectAclsByIds(List<Long> ids) {
        return aclMapper.selectAclsByIds(ids);
    }

    @Override
    public String InsertAclsByUserId(List<Long> ids, Long roleid) {
        List<Long> menuIds;
        List<AclTotalDto> aclDto= aclService.selectAclsByIds(ids);
        List<Acl> alist=new ArrayList<Acl>();
        aclService.delete(new EntityWrapper<Acl>().eq("Pid",roleid));
        for (AclTotalDto ato: aclDto) {
            Acl newacl=new Acl();
            newacl.setId(0L);
            newacl.setPid(roleid);
            newacl.setPtype("role");
            newacl.setRtype(ato.getSn());
            newacl.setAclstate(ato.toString());
            alist.add(newacl);
        }
aclService.insertOrUpdateBatch(alist);
        Object[] menupos=new Object[]{0,1,2};
        Long[] rIds=(Long[]) ids.toArray(new Long[0]);

       List<MenuRes> menuResLists= menuResMapper.selectList(new EntityWrapper<MenuRes>().in("menuPos",menupos).in("id",rIds));
        List<Acl> acl2List=new ArrayList<Acl>();
        for (MenuRes mr: menuResLists) {
            Acl newacl=new Acl();
               newacl.setId(0L);
               newacl.setPid(roleid);
                newacl.setRid(mr.getId());
                newacl.setPtype("role");
                newacl.setRtype("menu");
            acl2List.add(newacl);
        }
        aclService.insertBatch(acl2List);
        return "success";
    }

    @Override
    public List<Long> selectResIdsByRoleId(Long roleid) {
             List<Long> resIds=aclMapper.selectResMenuIdListByRoleId(roleid);
        Set<AclTotalDto> atdo=aclMapper.selectAclsList(roleid);
        for (AclTotalDto ato:atdo) {
            if (ato.getAcl() != 0) {
                List<MenuRes> menuResList = menuResMapper.selectList(new EntityWrapper<MenuRes>().eq("pSn", ato.getSn()));

                for (MenuRes mr : menuResList) {
                    if (mr.getPostbit() == null) {

                    }else {
                        if (permissionBitUtil.checkPermission(mr.getPostbit(), ato.getAclState()) == true) {
                            resIds.add(mr.getId());
                        }
                    }


                }

            }
        }

        return resIds ;
    }

    @Override
    public Set<AclTotalDto> selectAclsList(Long roleid) {
        return aclMapper.selectAclsList(roleid);
    }
}
